var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

const bodyParser = require("body-parser");
require("body-parser-xml")(bodyParser);

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");

// const { mongodb } = require('./config')
// const tokenModel = require('./model/tokenModel')
// const mongoose = require('mongoose')
// mongoose.connect(mongodb.uri, {
//   user: mongodb.user,
//   pass: mongodb.pass,
//   useNewUrlParser: true
// }, (err) => {
//   if (err) console.log(err.message);
//   else {
//     tokenModel.findOne({}, function (err, token) {
//       if (err) console.log(err.message);
//       else {
//         token.isValid();
//       }
//     })
//   }
// })

var app = express();
app.use(
  bodyParser.xml({
    limit: "1MB",
    xmlParseOptions: {
      normalize: true,
      normalizeTags: true,
      explicitArray: false,
    },
    verify: function (req, res, buf, encoding) {
      if (buf && buf.length) {
        req.rawBody = buf.toString(encoding || "utf8");
      }
    },
  })
);

// app.use(bodyParser.json());
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/users", usersRouter);

module.exports = app;
